﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using EventManager.Models;
using EventManager.Services;
using EventManager.ViewModels;

namespace EventManager.Controllers
{
    public class EventsController : Controller
    {
        private IEventsServices _eventsServices;


        public EventsController(IEventsServices eventsServices)
        {
            _eventsServices = eventsServices;
            ViewBag.SelectedPage = "Events/Index";
        }

        #region Actions

        //
        // GET: /Events/

        public ActionResult Index()
        {
            ViewData["events"] = GetEventsList();

            return View();
        }

        //
        // GET: /Events/Details/{id}

        public ActionResult Details(int id)
        {
            ViewData["selected"] = GetEventsList().Single(n => n.Id == id);

            return View();
        }


        //
        // GET: /Events/CreateEvent

        [HttpGet]
        public ActionResult CreateEvent()
        {
            var viewModel = _eventsServices.GetEventForCreate();
            return View(viewModel);
        }


        //
        // POST: /Events/CreateEvent

        [HttpPost]
        public ActionResult CreateEvent(EventViewModel viewModel)
        {
            if (ModelState.IsValid)
            {
                var res= _eventsServices. SaveEvent(viewModel);
                if (res)
                {
                    return RedirectToAction("Index", "Events");    
                }
                else
                {
                    ModelState.AddModelError("", "Pleas correct erros.");
                }
            }
            else
            {
                ModelState.AddModelError("", "Pleas correct erros.");               
            }

            var empty = _eventsServices.GetEventForCreate();
            viewModel.Categories = empty.Categories;

            return View(viewModel);
        }


        public JsonResult ValidateName(string name)
        {
            var result = _eventsServices.ChceckEventNameAvailability(name);
            return Json(result, JsonRequestBehavior.AllowGet);
        }

        #endregion


        #region Private Helpers

        private List<Event> GetEventsList()
        {
            return new List<Event>
            { 
                new Event(1, "Kolokwium - Matematyka", new DateTime(2012, 11, 12, 14, 10, 0), new DateTime(2012, 11, 12, 16, 10, 0)),
                new Event(2, "Kolokwium - Fizyka", new DateTime(2012, 11, 13, 14, 10, 0), new DateTime(2012, 11, 13, 16, 10, 0)),                
                new Event(3, "Urodziny kolegi", new DateTime(2012, 11, 17, 20, 0, 0), new DateTime(2012, 11, 18, 6, 30, 0)),
                new Event(4, "Rozmowa o prace", new DateTime(2012, 11, 16, 15, 0, 0), new DateTime(2012, 11, 15, 16, 0, 0))
            };
        }

        #endregion
    }
}
